package com.example.mybatis.business.impl;

import com.example.mybatis.business.ICityBusiness;
import com.example.mybatis.dao.ICityDao;
import com.example.mybatis.model.CityTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("cityBusiness")
public class CityBusiness implements ICityBusiness {
    @Autowired
    private ICityDao cityDao;

    @Override
    public List<CityTO> queryByParentCode(String parentCode){
        return cityDao.queryByParentCode(parentCode);
    }

    @Override
    public Map<String,List<CityTO>> queryByParentCodes(List<String> parentCodes) {
        List<CityTO> cityList = cityDao.queryByParentCodes(parentCodes);
        //把相同父节点的CityTO是对象放到同一个List中
        Map<String,List<CityTO>> resultMap = new HashMap<>();
        List<CityTO> sameParentList = null;
        if(cityList.size()>0){
            for(CityTO cityTO:cityList){
                if(!resultMap.containsKey(String.valueOf(cityTO.getParentCode()))){
                    sameParentList = new ArrayList<>();
                    resultMap.put(cityTO.getParentCode(),sameParentList);
                }else{
                    resultMap.get(cityTO.getParentCode()).add(cityTO);
                }
            }
        }
        return resultMap;
    }

    @Override
    public List<String> queryExistCityCodes(List<String> cityCodes) {
        return cityDao.queryExistCityCodes(cityCodes);
    }

    @Override
    public void batchAddCities(List<CityTO> cityTOS){
        cityDao.batchAddCities(cityTOS);
    }

    @Override
    public void batchUpdateCities(List<CityTO> cityTOS){
        cityDao.batchUpdateCities(cityTOS);
    }
}
